package com.jstyle.jclifexd.view.sleepview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.view.View;

import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.Sleepfordata;
import com.jstyle.jclifexd.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

public class AllSleepView extends View {
    List<Sleepfordata> ALLdata = new ArrayList<>();//数据源
    float startX = 0;//开始距离
    float stopX = 0;//结束距离
    float linewith, lineheight;//0.5*6像素标志线  总高度大约 27dp
    float allvalue, avgvalue;//当前最对10小时
    Context context;
    Paint back_ground;//背景画笔
    Paint linePaint, linePaint2;//绘制线条刻度
    Paint textPaint, textPaint2;//绘制文字刻度
    Paint viewPaint;

    int fenzhong = 0;//共计多少分钟数据

    int greencolor = Color.parseColor("#EAFF01");//标记颜色
    int defaultcolor =Color.parseColor("#999999");
    int allX = 0;//总宽度

    public AllSleepView(Context context) {
        super(context);

    }

    public AllSleepView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public AllSleepView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    float startx, endx;

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        back_ground.setShader(null);
        linePaint.setShader(null);
        linePaint2.setShader(null);
        textPaint.setShader(null);
        textPaint2.setShader(null);
        viewPaint.setShader(null);
        if (getWidth() > 0 && getHeight() > 0) {
            //绘制背景
            RectF baseRect = new RectF(startX, getHeight() - ScreenUtils.dip2px(context, 15f), ScreenUtils.getScreenWidth(context) - stopX, getHeight());
            canvas.drawRoundRect(baseRect, ScreenUtils.dip2px(context, 9f), ScreenUtils.dip2px(context, 9f), back_ground);
            if (ALLdata.size() > 0) {
                if (1 == ALLdata.size()) {
                    Bitmap baitmap = drawableToBitmap(context.getResources().getDrawable(R.drawable.sleep_one), ALLdata.get(0).getColor(), context, (int) avgvalue * ALLdata.get(0).getIndex());
                    canvas.drawBitmap(baitmap, startX, getHeight() - ScreenUtils.dip2px(context, 15f), viewPaint);
                    baitmap.recycle();
                } else {
                    for (int j = 0; j < ALLdata.size(); j++) {
                        viewPaint.setColor(ALLdata.get(j).getColor());
                        if (j != 0) {
                            startx = endx;
                            endx = endx + avgvalue * ALLdata.get(j).getIndex();
                        } else {
                            startx = startX;
                            endx = startx + avgvalue * ALLdata.get(j).getIndex();
                        }

                        if (0 == j || j == ALLdata.size() - 1) {
                            // Rect rect = new Rect( (int)startx, getHeight() - ScreenUtils.dip2px(context, 15f), (int)endx, getHeight());
                            Bitmap baitmap;
                            if (0 == j) {
                                baitmap = drawableToBitmap(context.getResources().getDrawable(R.drawable.sleep_left), ALLdata.get(j).getColor(), context, (int) (endx - startx));
                            } else {
                                baitmap = drawableToBitmap(context.getResources().getDrawable(R.drawable.sleep_right), ALLdata.get(j).getColor(), context, (int) (endx - startx));
                            }
                            canvas.drawBitmap(baitmap, startx, getHeight() - ScreenUtils.dip2px(context, 15f), viewPaint);
                            baitmap.recycle();
                        } else {
                            RectF rect = new RectF(startx, getHeight() - ScreenUtils.dip2px(context, 15f), endx, getHeight());
                            canvas.drawRect(rect, viewPaint);
                        }


                    }
                }


            }


            if (ALLdata.size() > 0) {
                if (fenzhong >= 330&&fenzhong<540) {
                    linePaint.setColor(greencolor);
                    textPaint.setColor(greencolor);
                } else if (fenzhong >= 540) {
                    linePaint.setColor(greencolor);
                    textPaint.setColor(greencolor);
                    linePaint2.setColor(greencolor);
                    textPaint2.setColor(greencolor);
                }
            } else {
                linePaint.setColor(defaultcolor);
                linePaint2.setColor(defaultcolor);
                textPaint.setColor(defaultcolor);
                textPaint2.setColor(defaultcolor);
            }

            //绘制刻度线 5.5 ，9
            linePaint.setStrokeWidth(linewith);
            canvas.drawLine(startX + (avgvalue * (5.25f * 60f)), getHeight() - ScreenUtils.dip2px(context, 15f) - lineheight, startX + avgvalue * (5.25f * 60f), getHeight() - ScreenUtils.dip2px(context, 15f), linePaint);
            canvas.drawLine(startX + (avgvalue * (8.75f * 60f)), getHeight() - ScreenUtils.dip2px(context, 15f) - lineheight, startX + avgvalue * (8.75f * 60f), getHeight() - ScreenUtils.dip2px(context, 15f), linePaint2);
            //绘制文字
            canvas.drawText("5.5", startX + (avgvalue * (5.25f * 60f) - getTextWith(textPaint, "5.5") / 2f), ScreenUtils.dip2px(context, 6.5f) / 2f + ScreenUtils.dip2px(context, 4f), textPaint);
            canvas.drawText("9", startX + (avgvalue * (8.75f * 60f) - getTextWith(textPaint, "9") / 2f), ScreenUtils.dip2px(context, 6.5f) / 2f + ScreenUtils.dip2px(context, 4f), textPaint2);

        }


    }


    /**
     * Drawable转换成一个Bitmap
     *
     * @param drawable drawable对象
     * @return
     */
    public static final Bitmap drawableToBitmap(Drawable drawable, int color, Context context, int with) {
        GradientDrawable background = (GradientDrawable) drawable;
        background.setColor(color);
        Bitmap.Config config = background.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;//
        Bitmap bitmap = Bitmap.createBitmap(with, ScreenUtils.dip2px(context, 15f),
                config);
        Canvas canvas = new Canvas(bitmap);
        background.setBounds(0, 0, with, ScreenUtils.dip2px(context, 15f));
        background.draw(canvas);
        return bitmap;
    }

    /**
     * 设置数据源
     */
    public void setDataSource(int myduration, List<Sleepfordata> aLLdata) {
        ALLdata.clear();
        this.fenzhong = 0;
        this.fenzhong = myduration >= 600 ? 600 : myduration;
        this.ALLdata = aLLdata;
        invalidate();

    }


    private void init() {
        startX = ScreenUtils.dip2px(context, 20f);
        stopX = ScreenUtils.dip2px(context, 90f);
        linewith = ScreenUtils.dip2px(context, 0.5f);
        lineheight = ScreenUtils.dip2px(context, 6f);
        allvalue = ScreenUtils.getScreenWidth(context) - (startX + stopX);//总数10小时 每份60分钟
        avgvalue = (ScreenUtils.getScreenWidth(context) - (startX + stopX)) / 600f;//总数10小时 每份60分钟

        back_ground = new Paint();
        back_ground.setColor(defaultcolor);
        back_ground.setDither(true); // 防抖动
        back_ground.setAntiAlias(true);// 抗锯齿

        linePaint = new Paint();
        linePaint.setColor(Color.parseColor("#ff19424f"));
        linePaint.setDither(true); // 防抖动
        linePaint.setAntiAlias(true);// 抗锯齿

        linePaint2 = new Paint();
        linePaint2.setColor(Color.parseColor("#ff19424f"));
        linePaint2.setDither(true); // 防抖动
        linePaint2.setAntiAlias(true);// 抗锯齿


        viewPaint = new Paint();
        viewPaint.setColor(defaultcolor);
        linePaint2.setDither(true); // 防抖动
        viewPaint.setAntiAlias(true);// 抗锯齿


        textPaint = new Paint();
        textPaint.setColor(Color.parseColor("#19424f"));
        textPaint.setDither(true); // 防抖动
        textPaint.setAntiAlias(true);// 抗锯齿
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setTextSize(ScreenUtils.dip2px(context, 9f));


        textPaint2 = new Paint();
        textPaint2.setColor(Color.parseColor("#19424f"));
        textPaint2.setDither(true); // 防抖动
        textPaint2.setAntiAlias(true);// 抗锯齿
        textPaint2.setStyle(Paint.Style.FILL);
        textPaint2.setTextSize(ScreenUtils.dip2px(context, 9f));
    }

    /**
     * 传入文字计算文字宽度
     *
     * @param paint
     * @param str
     * @return
     */
    private static int getTextWith(Paint paint, String str) {
        paint.measureText(str);
        Rect rect = new Rect();
        paint.getTextBounds(str, 0, str.length(), rect);
        return rect.width();
    }

}
